package com.wwfish.cmsui.modules.sysuser.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.nexustar.gwt.dashboard.client.AbstractPage;
import com.nexustar.gwt.dashboard.client.PageClient;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.button.MyButton;
import com.nexustar.gwt.widgets.client.ui.form.FormContainer;
import com.nexustar.gwt.widgets.client.ui.form.FormElement;
import com.nexustar.gwt.widgets.client.ui.form.FormElementProviderAdpter;
import com.nexustar.gwt.widgets.client.ui.iprovider.IPaginationModelProvider;
import com.nexustar.gwt.widgets.client.ui.panel.ContentPanel;
import com.nexustar.gwt.widgets.client.ui.panel.InfoPanel;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.nexustar.gwt.widgets.client.ui.textbox.FishLabelTextBox;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolItem;
import com.nexustar.gwt.widgets.client.ui.window.IMessageConfirmCall;
import com.nexustar.gwt.widgets.client.ui.window.Message;
import com.wwfish.cms.model.CMSConstants;
import com.wwfish.cms.model.sysuser.SysRoleDto;
import com.wwfish.cms.service.sysuser.SysRoleManager;
import com.wwfish.cmsui.dashboard.client.CMSDashboard;
import com.wwfish.cmsui.modules.common.client.BaseCMSPage;
import com.wwfish.cmsui.modules.common.client.CMSPageClient;
import com.wwfish.cmsui.modules.common.client.CMSRunAsyncCallBack;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;
import com.wwfish.gwt.reflection.client.ModelReflection;
import com.wwfish.gwt.test.client.Dashboard;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-19
 * Time: 9:10:28
 * To change this template use File | Settings | File Templates.
 */
public class RoleManagePage extends BaseCMSPage {
    private FormContainer searchForm;
    ViewTable table;

    @Override
    protected boolean isRunAsync() {
        return true;
    }

    @Override
    public void initWidgets(final DockPanel page) {
        GWT.runAsync(new CMSRunAsyncCallBack() {
            public void onSuccess() {
                page.setWidth("100%");
                VerticalPanel vp = new VerticalPanel();
                vp.setWidth("100%");
                vp.setHeight("100%");
                vp.add(getMenuBar());
                initTableList();
                ContentPanel tableListWrapper = new ContentPanel("角色列表");
                tableListWrapper.setWidth("100%");
                tableListWrapper.setHeight("100%");
                tableListWrapper.addContentWidget(table);
                vp.add(tableListWrapper);
                vp.setCellHeight(tableListWrapper, "96%");

                ContentPanel north = getSearchPanel();
                page.add(north, DockPanel.NORTH);
                page.setCellHeight(north, "45px");
                page.add(vp, DockPanel.CENTER);

            }
        });
    }

    private Widget getMenuBar() {
        ToolBar bar = new ToolBar();
        bar.setWidth("100%");
        ToolItem add = new ToolItem("添加");
        ToolItem edit = new ToolItem("编辑");

        ToolItem delete = new ToolItem("删除");
        bar.addToolItem(add);
        bar.addToolItem(edit);
        bar.addToolItem(delete);

        add.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                CMSDashboard.dispatchPage(RolePage.class.getName(), new CMSPageClient() {
                    @Override
                    protected void process(BaseCMSPage page) {
                        page.getModelManger().renderModel(null);
                    }
                });
            }
        });
        edit.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (table.getSelectedItem() != null)
                    CMSDashboard.dispatchPage(EditRolePage.class.getName(), new CMSPageClient() {
                        @Override
                        protected void process(BaseCMSPage page) {
                            page.getModelManger().renderModel(ModelReflection.clone(table.getSelectedItem().getUserObject()));
                        }
                    });
            }
        });
        delete.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (table.getSelectedItem() != null) {
                    Message.confirm("确认要删除?", "温馨提示", new IMessageConfirmCall() {
                        public void doExcute(boolean flag) {
                            if (flag) {
                                ServiceFactory.invoke(SysRoleManager.class.getName(), "delete", new Object[]{table.getSelectedItem().getUserObject()}, new LoadingAsyncCallback() {
                                    @Override
                                    protected void success(Object result) {
                                        deleteRow(table, table.getSelectedItem().getUserObject());
                                        InfoPanel.show("删除成功");
                                    }
                                });

                            }
                        }
                    });
                }
            }
        });
        return bar;
    }

    private void initTableList() {
        table = new ViewTable(getColsWaiter());
    }

    private ContentPanel getSearchPanel() {
        ContentPanel searchPanel = new ContentPanel("查询条件");
        searchPanel.setWidth("100%");
        final FishLabelTextBox sName = new FishLabelTextBox("角色名称:");
        final MyButton go = new MyButton("查询");
        go.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ddOutPageModel(null);
            }
        });
        searchForm = new FormContainer(2);
        searchForm.getModelManger().renderModel(new HashMap(1));
        searchForm.addElement(new FormElement(sName, new FormElementProviderAdpter() {
            public void getValue(Object object) {
                ((Map) object).put("name", sName.getValue());
            }

            public void setValue(Object object) {
                sName.setValue(((Map) object).get("name"));
            }
        }));

        searchForm.addWidget(go);
        searchPanel.addContentWidget(searchForm);

        return searchPanel;
    }

    private ColumnWaiter getColsWaiter() {

        ColumnExt co1 = new ColumnExt("角色名称", 200, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                SysRoleDto dto = (SysRoleDto) cell.getUserObject();
                cell.setText(dto.getName());
            }
        });

        ColumnExt co2 = new ColumnExt("描述", 250, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                SysRoleDto dto = (SysRoleDto) cell.getUserObject();
                cell.setText(dto.getDescription());
            }
        });
        ColumnExt co3 = new ColumnExt("创建时间", 350, new IColumnRender() {
            public void renderColumn(CellItem cell) {
                SysRoleDto dto = (SysRoleDto) cell.getUserObject();
                cell.setText(DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").format(dto.getCreateTime()));
            }
        });
//        ColumnExt co5 = new ColumnExt("状态", 100, new IColumnRender() {
//            public void renderColumn(CellItem cell) {
//                UUser cp = (UUser) cell.getUserObject();
//                cell.setWidget(cp.getStatus() != null && cp.getStatus().equals( Constants._CP_ACTIVE) ?
//                        new HTML("<font color='green'>" + "激活" + "</font>") : new HTML("<font color='red'>" + "未激活" + "</font>"));
//            }
//        });
        ColumnWaiter cw = new ColumnWaiter(new ColumnExt[]{co1, co2, co3});
        return cw;
    }

    @Override
    protected void ddOutPageModel(Object dto) {
        Map map = (Map) searchForm.getModelManger().getModel();
        map.put("type", CMSConstants._ROLE_TYPE_REALITY);
        ServiceFactory.invoke(SysRoleManager.class.getName(), "getAllRoles", new Object[]{map}, new LoadingAsyncCallback() {
            @Override
            protected void success(Object result) {
                table.getModelManger().renderModel(result);
            }
        });
    }


}
